import Vuex from 'vuex'
import runStatus from '@/net/runStatus'

Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    year: new Date().getFullYear(),
    userAdcd: '',
    adcd: '',
    adcdName: [],
    adcdId: [],
    energyType: 1,
    gridHead: [{ name: 'GSJC', value: '统计单位'}],
    gridData: [],
    energyTypeData: [
      { name: '行政区划', value: 1},
      { name: '厂家', value: 2}
    ],
    sort: 'RUN_CNT',
    sortData: [
      { name: '开机总数', value: 'RUN_CNT' },
      { name: '室内温度', value: 'SNWD_AVG' },
      { name: '室外温度', value: 'SWWD_AVG' },
      { name: '进水温度', value: 'JSWD_AVG' },
      { name: '出水温度', value: 'CSWD_AVG' }
    ],
    isShow: 0,
    isShowData: [
      { name: '不显示温度数据表', value: 0},
      { name: '显示温度数据表', value: 1}
    ]
  },
  mutations: {
    changeYear (state, value) {
      state.year = value
    },
    changeUserAdcd (state, value) {
      state.userAdcd = value
    },
    changeAdcd (state, value) {
      state.adcd = value
    },
    changeAdcdName (state, value) {
      state.adcdName = value
    },
    changeAdcdId (state, value) {
      state.adcdId = value
    },
    changeEnergyType (state, value) {
      state.energyType = value
    },
    changeGridHead (state, value) {
      state.gridHead = value
    },
    changeGridData (state, value) {
      state.gridData = value
    },
    changeSort (state, value) {
      state.sort = value
    },
    changeIsShow (state, value) {
      state.isShow = value
    }
  },
  actions: {
    getLedger ({commit, state}, vm) {
      vm.$notice.loading.show()
      runStatus.getRunStatus({
        T0ND: state.year,
        quyxzqhdm: state.adcd,
        sorttype: state.sort,
        groupBy : state.energyType
      }).then(res => {
        let arrHead = [
          { name: 'GSJC', value: '统计单位'},
          { name: 'SBCNT', value: '采集总数'},
          { name: 'RUN_CNT', value: '开机数'},
          { name: 'RUN_CNT_L', value: '开机率'},
          { name: 'FAULT_CNT', value: '故障数'},
          { name: 'FAULT_CNT_L', value: '故障率'},
          { name: 'CLOSE_CNT', value: '关机数'},
          { name: 'CLOSE_CNT_L', value: '关机率'}
        ]
        if (state.isShow === 1) { 
          arrHead = arrHead.concat([
            { name: 'JSWD_MAX', value: '进水温度(最高)'},
            { name: 'JSWD_MIN', value: '进水温度(最低)'},
            { name: 'JSWD_AVG', value: '进水温度(平均)'},
            { name: 'CSWD_MAX', value: '出水温度(最高)'},
            { name: 'CSWD_MIN', value: '出水温度(最低)'},
            { name: 'CSWD_AVG', value: '出水温度(平均)'},
            { name: 'SNWD_MAX', value: '室内温度(最高)'},
            { name: 'SNWD_MIN', value: '室内温度(最低)'},
            { name: 'SNWD_AVG', value: '室内温度(平均)'},
            { name: 'SWWD_MAX', value: '室外温度(最高)'},
            { name: 'SWWD_MIN', value: '室外温度(最低)'},
            { name: 'SWWD_AVG', value: '室外温度(平均)'}
          ])
        }
        res.datalist.forEach(value => {
          value.RUN_CNT_L = value.RUN_CNT_L + '%'
          value.FAULT_CNT_L = value.FAULT_CNT_L + '%'
          value.CLOSE_CNT_L = value.CLOSE_CNT_L + '%'
        })
        commit('changeGridHead', arrHead)
        commit('changeGridData', res.datalist)
        vm.$nextTick(() => {
          vm.$notice.loading.hide()
        })
      })
    }
  }
})
